package cn.com.infcn.monitor.task.system;

import org.apache.log4j.Logger;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

import cn.com.infcn.monitor.bean.IFCMem;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * 获取系统内存相关信息
 * 
 * @author jjs
 *
 */
public class SysMemTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(SysMemTask.class);

	@Override
	public IFCMem execute() throws Exception {
		return mem();
	}

	private IFCMem mem() throws SigarException {

		try {
			Sigar sigar = new Sigar();
			Mem m = sigar.getMem();
			IFCMem mem = new IFCMem();
			IFCBeanUtils.copyProperties(m, mem);

			Swap swap = sigar.getSwap();
			mem.setSwapFree(swap.getFree());
			mem.setSwapPageIn(swap.getPageIn());
			mem.setSwapPageOut(swap.getPageOut());
			mem.setSwapTotal(swap.getTotal());
			mem.setSwapUsed(swap.getUsed());

			return mem;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}
		return null;
	}

	/*private void copyProperties(Mem m, IFCMem mem){
		mem.setActualFree(m.getActualFree());
		mem.setActualUsed(m.getActualUsed());
		mem.setFree(m.getFree());
		mem.setFreePercent(m.getFreePercent());
		mem.setRam(m.getRam());
		mem.setTotal(m.getTotal());
		mem.setUsed(m.getUsed());
		mem.setUsedPercent(m.getUsedPercent());
	}*/
}
